Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming


Comparative procedures

This is the top-level super procedure that provides common error handler and time-tracking routines:

CommonProc.p
DEFINE SHARED VARIABLE timestamp AS DATETIME NO-UNDO. 
PROCEDURE updateTimestamp: 
    timestamp = NOW. 
END PROCEDURE. 
FUNCTION ErrorHandler RETURNS HANDLE (INPUT ProcType as CHARACTER). 
    DEFINE VARIABLE hError AS HANDLE NO-UNDO. 
    RUN ErrorProc.p PERSISTENT SET hError (INPUT ProcType). 
    RETURN hError. 
END FUNCTION. 

This is a procedure that extends CommonProc.p to provide general functionality for handling customers and is a super procedure for the NECustomer.p procedure, that handles New England customers:

CustProc.p
/* Main Block */                                                     /* 2 */ 
DEFINE SHARED VARIABLE iNumCusts AS INTEGER NO-UNDO. 
DEFINE TEMP-TABLE ttCust NO-UNDO 
    FIELD CustNum LIKE Customer.CustNum 
    FIELD Name LIKE Customer.Name 
    FIELD Email AS CHARACTER. 
DEFINE NEW SHARED VARIABLE timestamp AS DATETIME NO-UNDO.            /* 5 */ 
DEFINE VARIABLE hError AS HANDLE NO-UNDO. 
DEFINE VARIABLE hCommon AS HANDLE NO-UNDO. 
RUN CommonProc.p PERSISTENT SET hCommon.                             /* 1 */ 
THIS-PROCEDURE:ADD-SUPER-PROCEDURE(hCommon). 
FUNCTION ErrorHandler RETURNS HANDLE (INPUT ProcType as CHARACTER) IN SUPER. 
iNumCusts = 0. 
/* Fill temp table and get row count */ 
FOR EACH Customer: 
    CREATE ttCust. 
    ASSIGN 
        ttCust.CustNum = Customer.CustNum 
        ttCust.Name = Customer.Name 
    iNumCusts = iNumCusts + 1. 
END. 
hError = ErrorHandler(INPUT "CustProc").                             /* 3 */ 
PROCEDURE GetCustomerName: 
    DEFINE INPUT PARAMETER piCustNum AS INTEGER NO-UNDO. 
    DEFINE OUTPUT PARAMETER poName AS CHARACTER NO-UNDO. 
    FIND ttCust WHERE ttCust.CustNum = piCustNum NO-ERROR. 
    IF AVAILABLE ttCust THEN DO: 
        poName = ttCust.Name. 
    END. 
    ELSE DO: 
        RUN Alert IN hError(INPUT "No Customer with that number exists"). 
        poName = ?. 
    END. 
END PROCEDURE. 
PROCEDURE CheckCredit: 
    DEFINE VARIABLE hCreditProc AS HANDLE NO-UNDO. 
    RUN CreditProc.p PERSISTENT SET hCreditProc. 
    IF VALID-HANDLE(hCreditProc) THEN DO: 
        FOR EACH Customer where CustNum < 15: 
            IF Customer.CreditLimit < 15000 THEN DO: 
                RUN Alert IN hError  
                          (INPUT "There is a problem with this Customer"). 
                RUN CreditAlert in hCreditProc(INPUT Customer.CustNum). 
            END. 
        END. 
        DELETE OBJECT hCreditProc. 
    END. 
    ELSE DO: 
        RUN Alert in hError (INPUT "Unable to check credit"). 
    END. 
END PROCEDURE. 
PROCEDURE printProc: 
    OUTPUT TO PRINTER. 
    DISPLAY timestamp. 
    FOR EACH ttCust: 
        DISPLAY ttCust. 
    END. 
    OUTPUT CLOSE. 
END PROCEDURE. 
PROCEDURE logProc: 
    DEFINE INPUT PARAMETER fileName AS CHARACTER NO-UNDO. 
    OUTPUT TO VALUE(fileName). 
    DISPLAY timestamp. 
    FOR EACH ttCust: 
        DISPLAY ttCust. 
    END. 
    OUTPUT CLOSE. 
END PROCEDURE. 
PROCEDURE CleanUp: 
    EMPTY TEMP-TABLE ttCust. 
    DELETE OBJECT hError. 
    hError = ?. 
END PROCEDURE. 

This procedure extends CustProc.p to handle New England customers by overriding the GetCustomerName procedure to return the customer’s E-mail address along with their name:

NECustomer.p
DEFINE SHARED VARIABLE iNumCusts AS INTEGER NO-UNDO. 
DEFINE VARIABLE hCustProc AS HANDLE NO-UNDO. 
DEFINE NEW SHARED TEMP-TABLE ttCust NO-UNDO 
    FIELD CustNum LIKE Customer.CustNum 
    FIELD Name LIKE Customer.Name 
    FIELD Email AS CHARACTER. 
                                                                     /* 7 */ 
RUN CustProc.p PERSISTENT SET hCustProc.                             /* 8 */ 
THIS-PROCEDURE:ADD-SUPER-PROCEDURE(hCustProc). 
iNumCusts = 0. 
/* Fill temp table and get row count */ 
FOR EACH Customer: 
    CREATE ttCust. 
    ASSIGN 
        ttCust.CustNum = Customer.CustNum 
        ttCust.Name = Customer.Name 
        iNumCusts = iNumCusts + 1. 
    END. 
PROCEDURE GetCustomerName:                                          /* 9 */ 
    DEFINE INPUT PARAMETER piCustNum AS INTEGER NO-UNDO. 
    DEFINE OUTPUT PARAMETER poName AS CHARACTER NO-UNDO. 
    DEFINE VARIABLE CustName AS CHARACTER NO-UNDO. 
    RUN SUPER(INPUT piCustNum, OUTPUT CustName). 
    FIND FIRST ttCust WHERE CustNum = piCustNum NO-ERROR. 
    IF AVAILABLE(ttCust) THEN 
        RETURN ttCust.Name + ";" + ttCust.Email. 
END PROCEDURE. 

This procedure provides a notification of customers who have exceeded their credit limit:

CreditProc.p
PROCEDURE CreditAlert: 
    DEFINE INPUT PARAMETER iCustNum AS INTEGER NO-UNDO. 
    FIND FIRST Customer WHERE Customer.CustNum = iCustNum NO-ERROR. 
    IF AVAILABLE(Customer) THEN 
        MESSAGE Customer.Name " is on Credit Hold" SKIP 
                              "with a credit limit of " Customer.CreditLimit 
                              VIEW-AS ALERT-BOX. 
END PROCEDURE. 

This procedure provides a common error handler for other procedures to report error information:

ErrorProc.p
DEFINE INPUT PARAMETER ProcType AS CHARACTER NO-UNDO. 
PROCEDURE Alert: 
    DEFINE INPUT PARAMETER ErrorString AS CHARACTER NO-UNDO. 
    MESSAGE "Error in " ProcType "!" SKIP 
             ErrorString  
             VIEW-AS ALERT-BOX. 
END PROCEDURE. 

This procedure is the main driver for running all the other comparative procedures:

Main.p
DEFINE VARIABLE hCustProc AS HANDLE NO-UNDO. 
DEFINE VARIABLE oCustName AS CHARACTER NO-UNDO. 
DEFINE NEW SHARED VARIABLE iNumCusts AS INTEGER NO-UNDO. 
DEFINE TEMP-TABLE ttCustNames NO-UNDO 
    FIELD CustNum AS INTEGER 
    FIELD CustName AS CHARACTER 
    FIELD Email AS CHARACTER. 
DEFINE VARIABLE i AS INTEGER NO-UNDO. 
RUN CustProc.p PERSISTENT SET hCustProc. 
RUN updateTimestamp IN hCustProc.                                    /* 4 */ 
DO i = 1 to iNumCusts: 
    CREATE ttCustNames. 
    RUN GetCustomerName IN hCustProc(INPUT i, OUTPUT oCustName). 
    ASSIGN 
        ttCustNames.CustName = oCustName 
        ttCustNames.CustNum  = i. 
END. 
RUN CheckCredit IN hCustProc. 
RUN logProc IN hCustProc (INPUT "CustomerProc.out"). 
RUN CleanUp IN hCustProc.                                            /* 6 */ 
DELETE OBJECT hCustProc. 
hCustProc = ?. 


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095